{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 创建队列，并操作里面的元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "10\n",
      "1\n",
      "11\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 创建一个FIFO队列，队列中最多2个元素，类型为int32\n",
    "q = tf.FIFOQueue(2, \"int32\")\n",
    "# enqueue_many初始化队列中的元素\n",
    "init = q.enqueue_many(([0, 10], ))\n",
    "# dequeue把第一个元素0，出队列并保存到变量x\n",
    "x = q.dequeue()\n",
    "y = x + 1\n",
    "# enqueue把1插入到队列\n",
    "q_inc = q.enqueue([y])\n",
    "with tf.Session() as sess:\n",
    "    # 运行初始化队列\n",
    "    init.run()\n",
    "    for _ in range(5):\n",
    "        v, _ = sess.run([x, q_inc])\n",
    "        print(v)\n",
    "        \n",
    "# TensorFlow有两种队列：\n",
    "# tf.FIFOQueue：先进先出队列\n",
    "# tf.RandomShuffleQueue：随机调度队列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import threading\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 这个程序每隔1秒判断是否需要停止并打印自己的ID。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def MyLoop(coord, worker_id):\n",
    "    while not coord.should_stop():\n",
    "        if np.random.rand()<0.1:\n",
    "            print(\"Stoping from id: %d\\n\" % worker_id)\n",
    "            coord.request_stop()\n",
    "        else:\n",
    "            print(\"Working on id: %d\\n\" % worker_id)\n",
    "        time.sleep(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 创建、启动并退出线程。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Working on id: 0\n",
      "\n",
      "Working on id: 1\n",
      "Working on id: 2\n",
      "\n",
      "\n",
      "Working on id: 3\n",
      "\n",
      "Working on id: 4\n",
      "\n",
      "Working on id: 1\n",
      "Working on id: 2\n",
      "\n",
      "Working on id: 0\n",
      "Stoping from id: 4\n",
      "Working on id: 3\n",
      "\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 声明一个Coordinator类来协同多个线程\n",
    "coord = tf.train.Coordinator()\n",
    "# 声明创建5个线程\n",
    "threads = [threading.Thread(target=MyLoop, args=(coord, i, )) for i in range(5)]\n",
    "# 启动所有线程\n",
    "for t in threads:t.start()\n",
    "# 等待所有线程退出\n",
    "coord.join(threads)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
